#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

sbit fmq = P3^6; // ???????
sbit weixuan1 = P3^0; // ??????1
sbit weixuan2 = P3^1; // ??????2
sbit K12 = P3^4; // ?????T0
sbit K13 = P3^3; // ????INT0
sbit K14 = P3^7;

uchar x = 60;
uchar code seg_table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf}; // ???CA??????0-9?"-"

// 1ms??????
void delay1ms(uint i)
{
    uchar j;
    while (i--)
        for (j = 0; j < 125; j++);
}

// ???????
void display(uchar num)
{
    if (num == 60)
    {
        weixuan1 = 1;
        weixuan2 = 1;
        P0 = seg_table[10]; // ????-
        delay1ms(2);
    }
    else
    {
        // ??????
        // ??
        weixuan2 = 1;
        P0 = seg_table[num % 10];
        delay1ms(2);
        weixuan2 = 0;
        // ??
        weixuan1 = 1;
        P0 = seg_table[num / 10];
        delay1ms(2);
        weixuan1 = 0;
    }
}

// ??250HZ???????
void beep()
{
    fmq = 0;
    delay1ms(1);  // ??
    fmq = 1;      // ??0.25s
    delay1ms(1);  // ??
}

// ???????
void led_flow()
{
    uchar loop, i, j, temp;
    for (loop = 0; loop < 2; loop++)
    {
        temp = 0xfe;
        for (i = 0; i < 7; i++)
        {
            P2 = temp;
            delay1ms(500);
            temp = _crol_(temp, 1);
        }
        temp = 0x7f;
        for (j = 0; j < 7; j++)
        {
            P2 = temp;
            delay1ms(500);
            temp = _cror_(temp, 1);
        }
    }
    P2 = 0xfe;
    delay1ms(500);
    P2 = 0xff;
}

void Time0(void) interrupt 1
{
    static unsigned char n = 0;  // ??static???
    if (++n == 20)
    {
        x--;
        n = 0;
    }
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;
    if (x == 0)
        TR0 = 0;
}

void int1(void) interrupt 2
{
    EX1 = 0;
    x = 60;
    EX1 = 1;
}

void main()
{
    EA = 1;
    EX1 = 1;
    IT1 = 1;
    ET0 = 1;
    TMOD = 0x01;
    TH0 = (65536 - 50000) / 256;
    TL0 = (65536 - 50000) % 256;

    uchar keycode;  // ?keycode?????main????
    while (1)
    {
        P1 = 0xf0;
        uchar scan1 = P1;
        if (scan1 != 0xf0)
        {
            delay1ms(20);
            scan1 = P1;
            if (scan1 != 0xf0)
            {
                P1 = 0x0f;
                uchar scan2 = P1;
                keycode = scan1 | scan2;  // ????keycode??

                switch (keycode)
                {
                case (0xee): x = 0; break;
                case (0xde): x = 1; break;
                case (0xbe): x = 2; break;
                case (0x7e): x = 3; break;
                case (0xed): x = 4; break;
                case (0xdd): x = 5; break;
                case (0xbd): x = 6; break;
                case (0x7d): x = 7; break;
                case (0xeb): x = 8; break;
                case (0xdb): x = 9; break;
                case (0xbb): x = 10; break;
                case (0x7b): x = 11; break;
                }
                beep();
            }
        }
        if (K12 == 0)
        {
            delay1ms(15);
            if (K12 == 0)
                TR0 = 1;
            x = 60;
            while (!K12);
        }
        if (K14 == 0)
        {
            delay1ms(15);
            if (K14 == 0)
            {
                led_flow();
                while (!K14);
            }
        }
        display(x);
    }
}
 